package fr.iut2.tc1.premierprogramme.client;

import java.util.ArrayList;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.HorizontalPanel;

public class Galerie extends Composite {
	private int NB_images = 4;
	private int position= 0 ;
	private ArrayList <String> files;
	private Button buttonPrecedent = new Button("precedente");
	private Button buttonSuivant = new Button("suivante");
	private HorizontalPanel hPanel = new HorizontalPanel();
	private HorizontalPanel hPanelImg = new HorizontalPanel();
		
	private int precedent(){
		if(this.position<=0){
			position=files.size()-1;
		}else{
			position--;
		}
		return position;
		
		}
	private int getSuivant(){
		if(position==files.size()-1){
			position=0;
		}else{ 
			position++;
		}
		return position;
		}
	public int getPosition(){
		return this.position;
		
	}
	public void InitPrecedent(){
		// remet le pointeur position en place pour gérer l'affichage des 4 photos:
		// exemple : si on en est au bloc de photo [0,1,2,3] -> return 28
		// Utilisé 4 fois, getSuivant() peut ensuite afficher les images [29,0,1,2]
		for(int i =0;i<NB_images+1;i++){
			precedent();
		}
	}
	public void InitSuivant(){
		for(int i =0;i<(NB_images-1);i++){
			precedent();
		}
		
	}
	
	public Galerie(ArrayList<String> listPhotos) {
		files = listPhotos;
		// We can add style names
		buttonPrecedent.addStyleName("pc-template-btn");
		buttonSuivant.addStyleName("pc-template-btn");
		// or we can set an id on a specific element for styling
		
		for(int i =0;i<NB_images;i++){
			final Image img = new Image(GWT.getHostPageBaseURL()+"images/"+listPhotos.get(i));
			img.addStyleName("pc-template-img");
			img.setWidth("100px");
			img.setHeight("100px");
			img.setTitle(listPhotos.get(i));
			img.setAltText(listPhotos.get(i));
			hPanelImg.add(img);
			img.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					PremierProgramme.get().getPhotoShow().setImg(img);
				}
			});
		}
		position = NB_images - 1;
		System.out.println("position: "+position);

		
		//specified height
		hPanel.setHeight("200px");
	

		hPanel.add(buttonPrecedent);
		hPanel.add(hPanelImg);
		hPanel.add(buttonSuivant);
		hPanel.getElement().setId("pc-template-hPanel");
		// Add image and button to the RootPanel
		this.initWidget(hPanel);
		
		// Set the contents of the Widget
		buttonPrecedent.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				hPanelImg.clear();
				InitPrecedent();
				System.out.println("Init: "+getPosition());
				for(int i =0;i<NB_images;i++){
					String name = files.get(getSuivant());
					final Image img = new Image(GWT.getHostPageBaseURL()+"images/"+name);
					img.addStyleName("pc-template-img");
					img.setWidth("100px");
					img.setHeight("100px");
					img.setTitle(name);
					img.setAltText(name);
					hPanelImg.add(img);
					System.out.println("positionEncour: "+getPosition());
					img.addClickHandler(new ClickHandler() {
						public void onClick(ClickEvent event) {
							PremierProgramme.get().getPhotoShow().setImg(img);
						}
					});
				}	
		System.out.println("position: "+getPosition());

			}
		});
		buttonSuivant.addClickHandler(new ClickHandler() {
			
			public void onClick(ClickEvent event) {
						hPanelImg.clear();
						InitSuivant();
						System.out.println("Init: "+getPosition());
						for(int i =0;i<NB_images;i++){
							String name = files.get(getSuivant());
							final Image img = new Image(GWT.getHostPageBaseURL()+"images/"+name);
							img.addStyleName("pc-template-img");
							img.setWidth("100px");
							img.setHeight("100px");
							img.setTitle(name);
							img.setAltText(name);
							hPanelImg.add(img);
							System.out.println("positionEncour: "+getPosition());
							img.addClickHandler(new ClickHandler() {
								public void onClick(ClickEvent event) {
									PremierProgramme.get().getPhotoShow().setImg(img);
								}
							});
						}	
				System.out.println("position: "+getPosition());

			}
		});
		
	}
	
}
